package math;

public class GPSDemo {

    public static void main(String[] args) {
        System.out.println(getCalculateTheDistanceBetweenTwoPoints(112.723897,38.726817
                , 112.702082,38.728883));
    }

    private static double getCalculateTheDistanceBetweenTwoPoints(double longitude_1, double latitude_1,
                                                                  double longitude_2, double latitude_2) {
        // 纬度
        double lat1 = Math.toRadians(latitude_1);
        double lat2 = Math.toRadians(latitude_2);
        // 经度
        double lng1 = Math.toRadians(longitude_1);
        double lng2 = Math.toRadians(longitude_2);
        // 纬度之差
        double a = lat1 - lat2;
        // 经度之差
        double b = lng1 - lng2;
        // 计算两点距离的公式
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
        // 弧长乘地球半径, 返回单位: 米
        s = s * 6378137.0d;
        s = Math.round(s * 10000d) / 10000d;//精确距离的数值
        return s;
    }
}
